package com.bdk.middle.base.web;


import com.bdk.middle.base.api.domain.DictionariesDO;
import com.bdk.middle.base.api.query.DictionariesQueryDO;
import com.bdk.middle.base.service.DictionariesService;
import com.bdk.middle.common.domain.PageDO;
import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/dictionaries")
public class DictionariesController {

    @Autowired
    private DictionariesService DictionariesService;

    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    public @ResponseBody
    ObjectResponse add(@RequestBody DictionariesDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = DictionariesService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody DictionariesDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = DictionariesService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody DictionariesDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = DictionariesService.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody DictionariesDO domain) {

        ObjectResponse<DictionariesDO> response = new ObjectResponse();

        ResultDO result = DictionariesService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((DictionariesDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody DictionariesQueryDO domain) {

        ObjectResponse<PageDO<DictionariesDO>> response = new ObjectResponse();

        ResultDO result = DictionariesService.find(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((PageDO<DictionariesDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
}

